<?php

	global $HTTP_GET_VARS;
	global $HTTP_POST_VARS;
	global $account;
	global $account_state;
	global $error;
	global $sql_row, $sql, $sql_total;

	$game = $_GET['game'];
	
	if (strpos($game,"/",0) !== false||strpos($game,"\\",0) !== false)
	{
		die("Warning: Possible hacking attempt detected. Aborted.");	
	}
	
	if (file_exists('scripts/'.$game.'.php'))
	{
		include_once('scripts/'.$game.'.php');
		
		if (isset($_POST['PostingFeedback']))
		{
			$query = "SELECT count(*) as count FROM distribution_register WHERE username = '".$_POST['SteamUsername']."' AND product = '".$game."'";
						
			$slot	=	sql($query);
			
			if ( $sql_row[$slot]['count'] < 1 )
			{
				output('<p><b class="text_big" style="color: #FF0000;">The Username "'.$_POST['SteamUsername'].'" hasn\'t installed '.$game.'!</b><br />You need to have installed '.$game.' before you can post feedback on it. Sorry!</p>' );
			}
			else
			{
				$date = datadate();
				$query = sprintf("INSERT INTO distribution_feedback (username, product, posted, text) VALUES ('%s', '%s', %s, '%s')",
									   $_POST['SteamUsername'],
									   $game,
									   slash($date),
									   $_POST['Feedback']);
									   
				unset($_POST['Feedback']);
				
				sql_once($query);
			}
			clearslot(slot);
		}
		
		$keys			=	makedefaultkeywords();	
		dopage('Upload_StartRows',$keys);
		
				$keys			=	makedefaultkeywords();
		$keys			=	makekeywords('name',html2text($game),$keys);
		if (isset($about[$game]))
		{			
			$keys			=	makekeywords('desc',$about[$game],$keys);					
		}
		else
		{
			$keys			=	makekeywords('desc','This page contains information about '.$game,$keys);
		}		
		$keys			=	makekeywords('thumburl_100px',uploadid($logo_100x100[$game]),$keys);
		dopage('upload_row',$keys);
					
		$register_slot = sql("SELECT `progress`, COUNT(*) AS howmany FROM `distribution_register` WHERE `product` = '".$game."' GROUP BY `progress`");
	
		// Put together the numbers!
		
		$counter=0;
		$total=0;
		do
		{
		
			$array[$sql_row[$register_slot]['progress']] = $sql_row[$register_slot]['howmany'];
			$array_index[$counter] = $sql_row[$register_slot]['progress'];
			$total += $sql_row[$register_slot]['howmany'];

			$counter+=1;
			sql_next($register_slot);
		}
		while ($counter<$sql_total[$register_slot]);
		
		if ( $account_state == 1 && $account['admin'] == 1)
		{
			$before		=		'<a href="?action=showusers&amp;game='.$game.'">';
			$after		=		'</a>';
		}
		else
		{
			$before		=		'';
			$after		=		'';
		}
		
		if ($total > 1)
		{
			$num_download_str	=	 $before.$total . ' users have installed '.$game.$after;
		}
		elseif ($total == 0)
		{
			$num_download_str	=	 'Nobody has installed '.$game.$after;
		}
		else
		{
			$num_download_str	=	 $before.$total . ' user has installed '.$game.$after;
		}
		
		
		$keys			=	makedefaultkeywords();
		if (isset($download_link[$game]))
		{
			$password_protected		=	false;
			if (isset($game_password[$game]))
			{
				$password_protected		=	true;
			
				if ( isset($game_nopasswordafter[$game]) )
				{
					if ( time() > strtotime($game_nopasswordafter[$game]) )
					{
						$password_protected		=	false;
					}
				}
			}
			
			if ( $password_protected )
			{
				$keys			=	makekeywords('name','Download '.$game,$keys);
				$keys			=	makekeywords('desc','</p><form method="post" action="?action=DownloadInstaller&amp;game='.$game.'"><p>
This mod is password protected. You need a password to download it.<br />
<input type="password" name="GamePassword" />
<input type="submit" name="Download" value="Download" /></p></form><p>',$keys);
			}
			else
			{
				$keys			=	makekeywords('name','Download '.$game.' now!',$keys);
				//$keys			=	makekeywords('url',html2text($download_link[$game]),$keys);
				$keys			=	makekeywords('url',ROOT . ROOT2ROOT . 'distribution/?action=DownloadInstaller&amp;game='.$game,$keys);
				$keys			=	makekeywords('desc',$num_download_str,$keys);
			}
		
			
				
		}
		else
		{
			$keys			=	makekeywords('name','A download link for this game is not available yet',$keys);
			$keys			=	makekeywords('desc','',$keys);
		}		
		$keys			=	makekeywords('thumburl_100px',uploadid(1644),$keys);
		dopage('upload_row',$keys);

		// Edit this game!		
		
		if ( $account_state == 1 && ($account['id'] == $owner[$game] || $account['admin'] == 1))
		{
			$keys			=	makedefaultkeywords();
			$keys			=	makekeywords('name','Edit the settings for '.$game.'!',$keys);
			$keys			=	makekeywords('desc','Change everything about '.$game.', logo, maps, passwords, everything!',$keys);
			$keys			=	makekeywords('url','?action=editgame&amp;game='.$game,$keys);
			$keys			=	makekeywords('thumburl_100px',uploadid(1652),$keys);
			dopage('upload_row',$keys);
		}
		
		
		// End the rows!
			
		$keys			=	makedefaultkeywords();		
		dopage('Upload_EndRows',$keys);
				
		if ( isset($screenshots[$game]) )
		{
			output('<table><tr><td><p><b class="text_big">Screenshots</b><br />');
			$num	=	count($screenshots[$game])+1;
			for ($i = 1; $i < $num; $i++)
			{
				output('<div class="img"><a href="'.ROOT.ROOT2ROOT.'upload/?id='.html2text($screenshots[$game][$i]).'"><img src="'.thumbid($screenshots[$game][$i],192,192).'" alt="'.$game.'" /></a></div>');		
			}	
			output('</p></td></tr></table>');
		}	
			
		output('<p><br /><br />');
		
		if ( isset( $progress_id_to_value[$game] ) )
		{		
			// Do the actual output
			
			output( '<b class="text_big">Percentage of users who have reached specific map</b><br />
			Our systems track how many users have reached a specific map. This is very valuable information to developers,
			because they then easily can spot a map where many players get stuck, or just dislike.<br /><br />');
			
			if ( $account_state == 1 && $account['admin'] == 1)
			{
					$leftstr	=	'<a href="?action=showusers&amp;game='.$game.'">';
					$rightstr	=	'</a>';				
			}
			else
			{
					$leftstr	=	'';
					$rightstr	=	'';
			}
			
			output($leftstr . '<img src="?action=statgraph&game='.$game.'&graph=mapcompletion" alt="Stats for '.$game.'" style="border: 0px;" /><br /><br />' . $rightstr);
		}
		
		if ( isset($download_link[$game]) )
		{		
			// Do the actual output
			
			output( '<b class="text_big">Number of downloads</b><br />
			Our systems track how many users have installed our products and when. This is a good indication on how popular a game is and whether it\'s increasing in its popularity, or not.<br /><br />');
			
			if ( $account_state == 1 && $account['admin'] == 1)
			{
					$leftstr	=	'<a href="?action=showusers&amp;game='.$game.'">';
					$rightstr	=	'</a>';				
			}
			else
			{
					$leftstr	=	'';
					$rightstr	=	'';
			}
			
			output($leftstr . '<img src="?action=statgraph&game='.$game.'&graph=showdownloads" alt="Stats for '.$game.'" style="border: 0px;" /><br /><br />' . $rightstr);
		}
		
		// Feedback!
		
		$register_slot = sql("SELECT * FROM `distribution_feedback` WHERE `product` = '".$game."' ORDER BY posted DESC");
		
		output('<b class="text_big" >Feedback</b><br />
		The best way to improve a game is to get feedback on it, so help us improve our games by sending back your thoughts on it.<br /><br />');
		
		if ( $sql_total[$register_slot] == 0 )
		{
			output('Nobody has posted any feedback on '.$game.' yet, be the first to do so!<br />');			
		}
		else
		{		
			$counter=0;
			$total=0;
			do
			{
			
				//$sql_row[$register_slot]['progress']
				
				output('<b>Feedback posted on '.$sql_row[$register_slot]['posted']);
				$sql_row[$register_slot]['text']=str_replace("\n","<br />\n",html2text($sql_row[$register_slot]['text']));
				if ( $account_state == 1 && $account['admin'] == 1)
				{
					output(' by <a href="?action=showuser&amp;account='.$sql_row[$register_slot]['username'].'">'.$sql_row[$register_slot]['username'].'</a>');					
				}
				output('</b><br />'.$sql_row[$register_slot]['text'].'<br /><br />');				
	
				$counter+=1;
				sql_next($register_slot);
			}
			while ($counter<$sql_total[$register_slot]);
		}
		clearslot($register_slot);
	
		output('</p>');

		$keys			=	makedefaultkeywords();
		$keys			=	makekeywords('Game',html2text($game),$keys);
		$keys			=	makekeywords('Feedback',(isset($_POST['Feedback'])) ? html2text(stripslashes($_POST['Feedback'])) : '',$keys);
		dopage('Distribution_PostFeedback ',$keys);
	}
	else
	{	
		output('<p><b class="text_big">'.html2text($game).'</b><br />Sorry this game is not supported by Maxsi Distribution.</p>');		
	}
	

?>